package com.example.userlogon.controller;

import com.example.userlogon.entity.Goods;
import com.example.userlogon.service.GoodsService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller

public class GoodsController {

    @Autowired
    GoodsService goodsService;
    @Autowired
    HttpServletRequest request;
//    访问商品管理首页

    /***
     * 进入商品管理首页
     * @param model
     * @return
     */

    @RequestMapping("/goods")
    public String goods(Model model){
        List<Goods> goodsList = goodsService.findAll();
        System.out.println(goodsList);
        Object username=request.getSession().getAttribute("username");
        model.addAttribute("data",goodsList);
        model.addAttribute("username",username);
        return "goods";
    }

    /***
     * 进入添加商品的页面
     * @return
     */
    @RequestMapping("/goodsAdd")
    public String goodAdd(Model model){
        Object username=request.getSession().getAttribute("username");
        model.addAttribute("username",username);
        return "goodsAdd";
    }

    /**
     * 处理商品添加的方法
     * @param name
     * @param price
     * @param quantity
     * @param category
     * @param supplier
     * @return
     */
    @RequestMapping("/goodsAddProcess")
    public String goodsAddProcess(@RequestParam String name,@RequestParam String price,
                                  @RequestParam String quantity,@RequestParam String category,
                                  @RequestParam String supplier){
//      组装一个goods实体对象
        Goods goods=new Goods(null,name,Float.valueOf(price),Integer.valueOf(quantity),category,supplier,"0");
        goods.setName(name);
        goods.setPrice(Double.valueOf(price));
        goods.setQuantity(Integer.valueOf(quantity));
        goods.setCategory(category);
        goods.setSupplier(supplier);
        System.out.println(goods);

//        调用service的add方法给数据库
        if(goodsService.add(goods)==1) {
//        如果返回ok，那么就进入商品管理首页
            return "redirect:/goods";
        }else{
            return "redirect:/goods";
        }
    }

    /**
     * 更新某条商品的记录

     * @param id
     * @return
     */
//    处理更新商品，首先要给出一个页面显示该条记录的数据
    @RequestMapping("/goodsUpdate")
    public String goodsUpdate(@RequestParam String id,Model model){
//        首先基于id查询出来该id记录里的所有数据信息
        Goods goods=new Goods();
        goods.setId(Integer.valueOf(id));
//        调用service方法查询出来该记录的数据信息
        Goods goodsdb = goodsService.findOne(goods);
//        2.将这些数据信息返回给视图
        model.addAttribute("data",goodsdb);
//        3.返回视图文件
        return "goodsUpdate";
    }

    @RequestMapping("/goodsUpdateProcess")
    public String goodsUpdateProcess(@RequestParam String name,@RequestParam String price
            ,@RequestParam String number,@RequestParam String category,
                                     @RequestParam String supplier
    ) {
//      组装一个goods实体对象
        Goods goods = new Goods();
        goods.setName(name);
        goods.setPrice(Double.valueOf(price));
        goods.setQuantity(Integer.valueOf(number));
        goods.setCategory(category);
        goods.setSupplier(supplier);
        System.out.println(goods);

//        调用service的add方法给数据库
        if (goodsService.update(goods) == 1) {
//        如果返回ok，那么就进入商品管理首页
            return "redirect:/goods";
        } else {
            return "redirect:/goods";
        }
    }
}
